package com.sunjob.mrologdb.service;

import com.alibaba.fastjson.JSON;
import com.sunjob.common.domain.system.SysBusinessLog;
import com.sunjob.common.domain.system.SysBusinessLogContent;
import com.sunjob.common.mroAnno.LogModuleType;
import com.sunjob.mrologdb.mapper.SysBusinessLogMapper;
import org.apache.commons.beanutils.BeanUtils;
import org.bouncycastle.util.Times;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.sql.Timestamp;
import java.util.*;

/**
 * @author LongJunJiang
 * @create 2022-08-10-15:11
 */
@Service
public class BusinessLogServiceImpl implements BusinessLogService {
    @Autowired
    SysBusinessLogMapper sysBusinessLogMapper;

    public static void map2SysBusinessLog(Map<String, Object> map, SysBusinessLog sysBusinessLog) {
        Set<Map.Entry<String, Object>> sets = map.entrySet();
        for (Map.Entry<String, Object> entry : sets) {
            Object value = entry.getValue();
            if (value != null) {
                try {
                    if (("businessLogContent".equals(entry.getKey()))) {
                        SysBusinessLogContent sysBusinessLogContent = JSON.parseObject((String) value, SysBusinessLogContent.class);
                        BeanUtils.setProperty(sysBusinessLog, entry.getKey(), sysBusinessLogContent);
                    } else if ("businessLogTime".equals(entry.getKey())) {
                        Timestamp time = (Timestamp) value;
                        BeanUtils.setProperty(sysBusinessLog, entry.getKey(), time.toLocalDateTime());
                    } else if ("businessLogStatus".equals(entry.getKey())) {
                        setSysBusinessLogStatus(sysBusinessLog, (int) value);
                    } else if ("businessLogModule".equals(entry.getKey())) {
                        setSysBusinessLogModule(sysBusinessLog, (String) value);
                    } else {
                        BeanUtils.setProperty(sysBusinessLog, entry.getKey(), value);
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void setSysBusinessLogStatus(SysBusinessLog sysBusinessLog, int status) {
        switch (status) {
            case 0:
                sysBusinessLog.setBusinessLogStatus(SysBusinessLog.BusinessStatus.SUCCESS);
                break;
            case 1:
                sysBusinessLog.setBusinessLogStatus(SysBusinessLog.BusinessStatus.FAIL);
                break;
            case 2:
                sysBusinessLog.setBusinessLogStatus(SysBusinessLog.BusinessStatus.UNKNOWN);
                break;
        }
    }

    @Override
    public Map queryBusinessMapLogById(Long businessLogId) {
        return sysBusinessLogMapper.queryBusinessLogById(businessLogId);
    }

    public static void setSysBusinessLogModule(SysBusinessLog sysBusinessLog, String module) {
        switch (module) {
            case "技术管理":
                sysBusinessLog.setBusinessLogModule(LogModuleType.TECHNOLOGY);
                break;
            case "资源管理":
                sysBusinessLog.setBusinessLogModule(LogModuleType.RESOURCE);
                break;
            case "现场执行":
                sysBusinessLog.setBusinessLogModule(LogModuleType.EXECUTE);
                break;
            case "计划管理":
                sysBusinessLog.setBusinessLogModule(LogModuleType.PLAN);
                break;
            case "资产管理":
                sysBusinessLog.setBusinessLogModule(LogModuleType.ASSETS);
                break;
            case "履历管理":
                sysBusinessLog.setBusinessLogModule(LogModuleType.RECORD);
                break;
            case "鉴权中心":
                sysBusinessLog.setBusinessLogModule(LogModuleType.AUTHEN);
                break;
            case "图片管理":
                sysBusinessLog.setBusinessLogModule(LogModuleType.IMG);
                break;
        }

    }

    @Override
    public List<SysBusinessLog> queryAllBusinessLog() {
        List<Map<String, Object>> lists_map = sysBusinessLogMapper.queryAllBusinessLog();
        List<SysBusinessLog> lists_object = new ArrayList<>();
        for (Map<String, Object> map : lists_map) {
            SysBusinessLog sysBusinessLog = new SysBusinessLog();
            BusinessLogServiceImpl.map2SysBusinessLog(map, sysBusinessLog);
            lists_object.add(sysBusinessLog);
        }
        return lists_object;
    }

    @Override
    public SysBusinessLog queryBusinessLogById(Long businessLogId) {
        Map<String, Object> map = sysBusinessLogMapper.queryBusinessLogById(businessLogId);
        SysBusinessLog sysBusinessLog = new SysBusinessLog();
        BusinessLogServiceImpl.map2SysBusinessLog(map, sysBusinessLog);
        return sysBusinessLog;
    }

    @Override
    public List<Map> querySysBusinessLogBy(SysBusinessLog sysBusinessLog) {
        Map<String, Object> map = new HashMap();
        if (sysBusinessLog.getSt() != null) {
            map.put("businessLogStatus", sysBusinessLog.getSt());
        }
        SysBusinessLog.BusinessStatus businessLogStatus = sysBusinessLog.getBusinessLogStatus();
        if (sysBusinessLog.getLm() != null) {
            map.put("businessLogModule", sysBusinessLog.getLm());
        }
        if (sysBusinessLog.getParams() != null) {
            map.put("params", sysBusinessLog.getParams());
        }
        return sysBusinessLogMapper.querySysBusinessLogBy(map);
    }

}
